﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Order
{
    /// <summary>
    /// OrderChangeDeliveryModifyWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OrderChangeDeliveryModifyWindow : Window
    {

        public OrderChangeViewModel OrderChange { get; set; }

        public OrderChangeDeliveryModifyWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                string[] com = Domain.DeliveryCompanyHelper.GetDeliveryCompany();
                this.cbbDeliveryCompany.ItemsSource = com;
                if (string.IsNullOrWhiteSpace(this.OrderChange.OrderChange.ReturnDeliveryCompany) == false)
                {
                    string cc = this.OrderChange.OrderChange.ReturnDeliveryCompany.Replace("快递", "").Replace("速递", "").Replace("快运", "");
                    string co = com.FirstOrDefault(obj => obj.Contains(cc));
                    if (string.IsNullOrWhiteSpace(co) == false)
                    {
                        this.cbbDeliveryCompany.SelectedItem = co;
                    }
                }
                this.tbDeliveryNumber.Text = this.OrderChange.OrderChange.ReturnDeliveryNumber;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string deliveryCompany = this.cbbDeliveryCompany.Text.Trim();
                if (string.IsNullOrWhiteSpace(deliveryCompany))
                {
                    MessageBox.Show("选择物流公司");
                    return;
                }
                string deliveryNumber = this.tbDeliveryNumber.Text.Trim();
                if (string.IsNullOrWhiteSpace(deliveryNumber))
                {
                    if (MessageBox.Show("快递单号为空，请确认?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                    {
                        return;
                    }
                }

                ServiceContainer.GetService<OrderChangeService>().UpdateDeliveryInfo(this.OrderChange.OrderChange.Id, deliveryCompany, deliveryNumber);
                MessageBox.Show("已成功");
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
